/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uns.fit.gis.xml;

import java.util.Stack;
import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author nPLUS
 */
public class BasicHandler extends DefaultHandler {
    private Vector persons;
    private Stack tagStack;

    public BasicHandler() {
        persons = new Vector();
        tagStack = new Stack();
    }

    public void startDocument() throws SAXException {
    }

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        //TODO: do something to process qName
        if (qName.equals("Person")) {
            persons.addElement(new Person());
        }
        tagStack.push(qName);
    }

    public void characters(char[] ch, int start, int length) throws SAXException {
        String chars = new String(ch, start, length).trim();
        if (chars.length() > 0) {
            String qName = (String) tagStack.pop();
            Person person = (Person) persons.lastElement();
            if (qName.equals("Name")) {
                person.setName(chars);
            } else if (qName.equals("Lattitude")) {
                person.setLattitude(Double.valueOf(chars).doubleValue());
            } else if (qName.equals("Longitude")) {
                person.setLongitude(Double.valueOf(chars).doubleValue());
            }
        }
    }

    public void endElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        tagStack.pop();
    }

    public void endDocument() throws SAXException {
        StringBuffer result = new StringBuffer();
        for(int i = 0; i < persons.size(); i ++) {
            Person person = (Person)persons.elementAt(i);
            result.append(person.getName());
            result.append("\n");
            result.append(person.getLattitude());
            result.append("\n");
            result.append(person.getLongitude());
            result.append("\n");
        }
        System.out.println(result.toString());
    }
}
